define(function(require, exports, module){
	require("resource/js/easyui/themes/icon.css");
	require("resource/js/easyui/easyloader.js");
	var AJAX = require("resource/js/component/ajax.js");
	var EUtil = require("resource/js/component/easyutil.js");
	var alerts = require("resource/js/component/alerts.js");
	var initpage = function(){
		var dataList="";
		$(".marginTop").html("");
		AJAX.init().success(function(c,m,d){
			if(c==0){
				$.each(d.layerAndMaterial, function(index, value){
					dataList += '<div><div id="'+value.layerId+'"><span>'+value.layerName+'</span><span class="updLayerBtn">修改</span>&nbsp;<span class="delLayerBtn">删除</span></div><div id="'+value.layerId+'" res="'+value.layerName+'">';
					$.each(value.items, function(i, val){
						dataList += '<span class="materialList" id="'+val.id+'"><input type="text" class="input_" readonly value="'+val.materialName+'" style="width: 100%;border: 0;padding: 5px;text-align: center;"/>'+
										'<span ids="'+val.id+'">'+
											'<i class="upd_o">修改</i>'+
											'<i>删除</i>'+
										'</span>'+
										'<img class="del_" src="'+BASEPATH+'/resource/images/guanbi03.png">'+
									'</span>';
					});
					dataList+="<span class='icon'><img src='"+BASEPATH+"/resource/images/add.png'/></span></div></div>";
				});
				$(".marginTop").append(dataList);
				$(".materialList").unbind("click");
				$(".materialList").click(function(){
					var this_ = $(this);
					$(this).find("span").toggle();
					$(this).siblings(".materialList").find("span").hide();
					this_.find(".upd_o").unbind("click");
					this_.find(".upd_o").click(function(){
						var val_ = this_.find("input").val();
						this_.find("input").focus();
						this_.find("input").removeAttr("readOnly");
						var obj={};
						obj.id=$(this).parent().attr("ids");
						this_.find("input").off("blur");
						this_.find("input").on("blur",function(){
							if(val_!=this_.find("input").val()){
								obj.materialName=this_.find("input").val();
								AJAX.init().success(function(code,msg,data){
									if(code==0){
										$.messager.show({ title:"系统提示", msg:msg, timeout:2000, showType:'slide' });
										this_.find("input").attr("readonly",true);
										initpage();
									}
								}).error(function(code,msg,data){
									$.messager.show(msg);
								}).contentType('application/json').post(BASEPATH+"/dcs/sys/material/updateSysMaterial.htm",JSON.stringify(obj));
							}else{
								initpage();
							}
						})
					})
					$(this).find("span").find("i").last().unbind("click");
					$(this).find("span").find("i").last().click(function(){
						$(this).parent().next("img").show();
						var ids=$(this).parent().attr("ids");
						alerts.confirm('是否确认删除?', function(flag) {
							if (flag) {
								var obj={};
								obj.id=ids;
								EUtil.ajaxHandler(BASEPATH+"/dcs/sys/material/deleteSysMaterial.htm", obj, function(c, m) {
									if (0 == c) {
										$(".del_").hide();
										this_.remove();
									} else {
										$.messager.show({title:'系统提示', msg:m});
									}
								})
							}else{
								$(".del_").hide();
							}
						})
					})
				})
				$(".materialList").find("span").unbind("hover");
				$(".materialList").find("span").hover(function(){
				},function(){
					$(this).hide();
				})
			}
			/*点击修改结构层*/
			$(".updLayerBtn").on("click",function(){
				var layerId = $(this).parent().attr("id");
				updLayer(layerId);
			})
			/*点击删除结构层*/
			$(".delLayerBtn").on("click",function(){
				var layerId = $(this).parent().attr("id");
				delLayer(layerId);
			})
			/*添加材料类型*/
			$(".icon").on("click",function(){
				var layerId = $(this).parent().attr("id");
				var layerName = $(this).parent().attr("res");
				addMaterial(layerId,layerName);
			})
			/*右键单击材料类型*/
			/*$(".materialList").mousedown(function(e) {
	            //右键为3
	            if (3 == e.which) {
	                alert("11111");
	            }
	        })*/
		}).post(BASEPATH+"/dcs/sys/layer/queryLayerAndMaterialList.htm");
	}
	
	/*新建结构层*/
	$("#addLayerBtn").on("click",function(){
		$("#layerEditdlg").dialog({
			title:"结构层添加",
			href:BASEPATH+"/view/dcs/sys/layerAndMaterial/layeradd",
			onLoad:function(){
		    },
			buttons: [{
				text:'保存',
				iconCls:'icon-ok',
				accessKey:{akey:18,mkey:83},
				handler:function(){
					using(["form"],function(){
						if($("#layerEditform").form("validate")){
							var obj = $("#layerEditform").serializeJson();
							AJAX.init().success(function(code,msg,data){
								$.messager.show({ title:"系统提示", msg:msg, timeout:2000, showType:'slide' });
								location.reload();
								$("#layerEditdlg").dialog("close");
		    	    		}).error(function(code,msg,data){
		    	    			$.messager.alert("系统提示", msg, "error");
		    	    		}).contentType('application/json').post(BASEPATH+"/dcs/sys/layer/saveSysLayer.htm",JSON.stringify(obj));
						}
					});
				}
			},{
				text:'取消',
				addClass:'l-btn-red',iconCls:'icon-not',
				accessKey:{akey:18,mkey:67},
				handler:function(){
					$('#layerEditdlg').dialog('close');
				}
			}]
		});
		$("#layerEditdlg").dialog("open");
	});
	
	/*修改结构层*/
	function updLayer(layerId){
		$("#layerEditdlg").dialog({
			title:"结构层添加",
			href:BASEPATH+"/view/dcs/sys/layerAndMaterial/layerupd",
			onLoad:function(){
				var obj={};
				obj.id=layerId;
				AJAX.init().success(function(c,m,d){
					if(c==0){
						$("#layerId").val(d.id);
						$("#layerName_upd").textbox("setValue",d.layerName);
					}
				}).post(BASEPATH+"/dcs/sys/layer/querySysLayer.htm",obj);
		    },
			buttons: [{
				text:'保存',
				iconCls:'icon-ok',
				accessKey:{akey:18,mkey:83},
				handler:function(){
					using(["form"],function(){
						if($("#layerEditform").form("validate")){
							var obj = $("#layerEditform").serializeJson();
							obj.id=$("#layerId").val();
							AJAX.init().success(function(code,msg,data){
								$.messager.show({ title:"系统提示", msg:msg, timeout:2000, showType:'slide' });
								location.reload();
								$("#layerEditdlg").dialog("close");
		    	    		}).error(function(code,msg,data){
		    	    			$.messager.alert("系统提示", msg, "error");
		    	    		}).contentType('application/json').post(BASEPATH+"/dcs/sys/layer/updateSysLayer.htm",JSON.stringify(obj));
						}
					});
				}
			},{
				text:'取消',
				addClass:'l-btn-red',iconCls:'icon-not',
				accessKey:{akey:18,mkey:67},
				handler:function(){
					$('#layerEditdlg').dialog('close');
				}
			}]
		});
		$("#layerEditdlg").dialog("open");
	}
	
	/*删除结构层*/
	function delLayer(layerId){
		alerts.confirm('是否确认删除?', function(flag) {
			if (flag) {
				var obj={};
				obj.id=layerId;
				EUtil.ajaxHandler(BASEPATH+"/dcs/sys/layer/deleteSysLayer.htm", obj, function(c, m) {
					if (0 == c) {
						location.reload();
					} else {
						$.messager.show({title:'系统提示', msg:m});
					}
				})
			}
		})
	}
	/*添加材料类型*/
	function addMaterial(id,name){
		$("#materialEditdlg").dialog({
			title:"材料类型添加",
			href:BASEPATH+"/view/dcs/sys/layerAndMaterial/materialadd",
			onLoad:function(){
				$("#layerId").val(id);
				$("#layerName_add").textbox("setValue",name);
		    },
			buttons: [{
				text:'保存',
				iconCls:'icon-ok',
				accessKey:{akey:18,mkey:83},
				handler:function(){
					using(["form"],function(){
						if($("#materialEditform").form("validate")){
							var obj = $("#materialEditform").serializeJson();
							AJAX.init().success(function(code,msg,data){
								$.messager.show({ title:"系统提示", msg:msg, timeout:2000, showType:'slide' });
								location.reload();
								$("#materialEditdlg").dialog("close");
		    	    		}).error(function(code,msg,data){
		    	    			$.messager.alert("系统提示", msg, "error");
		    	    		}).contentType('application/json').post(BASEPATH+"/dcs/sys/material/saveSysMaterial.htm",JSON.stringify(obj));
						}
					});
				}
			},{
				text:'取消',
				addClass:'l-btn-red',iconCls:'icon-not',
				accessKey:{akey:18,mkey:67},
				handler:function(){
					$('#materialEditdlg').dialog('close');
				}
			}]
		});
		$("#materialEditdlg").dialog("open");
	}
	
	module.exports = {
			trans:function(){
				using(["parser","dialog","textbox"],function(){
					initpage();
				});
			}
	}
});
